package cn.wolfcode.web.controller;

import cn.wolfcode.domain.JsonRequest;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;

    @RequestMapping("/list")

    public String getList(Model model, QueryObject queryObject){
        PageInfo<Permission> list = iPermissionService.selectlimit(queryObject);
        model.addAttribute("pageInfo",list);
        return "permission/list";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门编辑",expression = "/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        if (permission.getId()!=null){
            iPermissionService.updateByPrimaryKey(permission);
        }else {
            iPermissionService.insert(permission);
        }
        return "redirect:/permission/list";
    }

    //    删除
    @RequestMapping("/delete")
    @RequiredPermission(name = "部门删除",expression = "/delete")
    public  String delete(Long id){
        if (id!=null){
            iPermissionService.deleteByPrimaryKey(id);
        }
        return "redirect:/permission/list";
    }

    //    新增/修改
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id!=null){
            model.addAttribute("permission",iPermissionService.selectByPrimaryKey(id));
        }
        return "/department/input";
    }

//    查询所有的有权限的方法
    @RequestMapping("/reload")
    @RequiredPermission(name = "权限加载",expression = "/list")
    @ResponseBody
    public JsonRequest reload(){
        iPermissionService.reload();
        return new JsonRequest();
    }



}
